package com.zzyl.nursing.controller;

import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * deviceController
 *
 * @author ruoyi
 * @date 2025-10-23
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "device相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;


    @PostMapping("/queryDevicePropertyStatus")
    public AjaxResult queryDevicePropertyStatus(@RequestBody DeviceDto deviceDto){
        return success(deviceService.queryDevicePropertyStatus(deviceDto.getDeviceName(),deviceDto.getProductKey()));
    }


    @PostMapping("/queryProductModelInfo")
    public AjaxResult queryProductModelInfo(@RequestBody DeviceDto deviceDto){

        return success(deviceService.queryProductModelInfo(deviceDto));
    }



    @PostMapping("/queryDeviceDetail")
    public AjaxResult queryDeviceDetail(@RequestBody DeviceDto dto){
        DeviceDetailVo deviceDetailVo = deviceService.queryDeviceDetail(dto);
        return success(deviceDetailVo);
    }



    /**
     * 注册设备
     *
     * @param deviceDto
     * @return
     */
    @PostMapping("/register")
    @ApiOperation(value = "注册设备")
    public AjaxResult registerDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.register(deviceDto);
        return success();
    }


    /**
     * 同步产品列表
     *
     * @return
     */
    @PostMapping("/syncProductList")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return AjaxResult.success();
    }

    /**
     * 查询所有产品列表
     *
     * @return
     */
    @GetMapping("/allProduct")
    @ApiOperation(value = "查询所有产品列表")
    public AjaxResult allProduct() {
        List<ProductVo> list = deviceService.allProduct();
        return success(list);
    }

    /**
     * 查询device列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取device列表")
    public TableDataInfo<List<Device>> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 导出device列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:export')")
    @Log(title = "device", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出device列表")
    public void export(HttpServletResponse response, Device device) {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "device数据");
    }

    /**
     * 获取device详细信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取device详细信息")
    public R<Device> getInfo(@ApiParam(value = "deviceID", required = true) @PathVariable("id") Long id) {
        return R.ok(deviceService.selectDeviceById(id));
    }

    /**
     * 新增device
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "device", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增device")
    public AjaxResult add(@ApiParam(value = "device实体", required = true) @RequestBody Device device) {
        return toAjax(deviceService.insertDevice(device));
    }

    /**
     * 修改device
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:edit')")
    @Log(title = "device", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改device")
    public AjaxResult edit(@ApiParam(value = "device实体", required = true) @RequestBody DeviceDto dto) {
        return toAjax(deviceService.updateDevice(dto));
    }

    /**
     * 删除device
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:remove')")
    @Log(title = "device", businessType = BusinessType.DELETE)
    @DeleteMapping()
    @ApiOperation("删除device")
    public AjaxResult remove(@RequestBody DeviceDto dto) {
        return toAjax(deviceService.deleteDeviceById(dto));
    }
}
